package cn.edu.ujn.food.service;

import cn.edu.ujn.food.entity.Orderdetail;
import cn.edu.ujn.food.dao.OrderdetailDao;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

@Service("orderdetailService")
public class OrderdetailServiceImpl implements OrderdetailService {
    @Resource
    private OrderdetailDao orderdetailDao;

    @Override
    public Orderdetail queryById(Integer detId) {
        return this.orderdetailDao.queryById(detId);
    }

    @Override
    public Page<Orderdetail> queryByPage(Orderdetail orderdetail, PageRequest pageRequest) {
        long total = this.orderdetailDao.count(orderdetail);
        return new PageImpl<>(this.orderdetailDao.queryAllByLimit(orderdetail, pageRequest), pageRequest, total);
    }

    @Override
    public Orderdetail insert(Orderdetail orderdetail) {
        this.orderdetailDao.insert(orderdetail);
        return orderdetail;
    }

    @Override
    public boolean deleteById(Integer detId) {
        return this.orderdetailDao.deleteById(detId) > 0;
    }

    @Override
    public cn.edu.ujn.util.Page<Orderdetail> selectAll(Orderdetail orderdetail) {
        List<Orderdetail> orderdetails = orderdetailDao.selectAll(orderdetail);
        cn.edu.ujn.util.Page<Orderdetail> page = new cn.edu.ujn.util.Page<>();
        page.setPage(orderdetail.getStart());
        page.setRows(orderdetails);
        page.setSize(orderdetail.getRows());
        page.setTotal(orderdetailDao.selectCount());
        return page;
    }
}
